//
//  NFIFrame.m
//  CustomWindow
//
//  Created by Sean Patrick O'Brien on 9/15/06.
//  Copyright 2006 Sean Patrick O'Brien. All rights reserved.
//

#import "NFIFrame.h"
#import "CTGradient.h"
#import "EtchedTextCell.h"

static CTGradient * fillGradient = nil;
static CTGradient * edgeGradient = nil;


@implementation NFIFrame



+ (void)initialize
{
    fillGradient = [[CTGradient gradientWithBeginningColor: [[self class] fillGradientStartColor]
                                                 endingColor: [[self class] fillGradientEndColor]] retain];
    edgeGradient = [[CTGradient gradientWithBeginningColor: [[self class] edgeGradientStartColor]
                                               endingColor: [[self class] edgeGradientEndColor]] retain];
}



+ (NSBezierPath *)_clippingPathForFrame:(NSRect)aRect
{
    NSBezierPath * path = [NSBezierPath new];
    NSPoint topMid = NSMakePoint(NSMidX(aRect), NSMaxY(aRect));
    NSPoint topLeft = NSMakePoint(NSMinX(aRect), NSMaxY(aRect));
    NSPoint topRight = NSMakePoint(NSMaxX(aRect), NSMaxY(aRect));
    NSPoint bottomRight = NSMakePoint(NSMaxX(aRect), NSMinY(aRect));
    
    [path moveToPoint: topMid];
    [path appendBezierPathWithArcFromPoint: topRight
                                   toPoint: bottomRight
                                    radius: [self cornerRadius]];
    [path appendBezierPathWithArcFromPoint: bottomRight
                                   toPoint: aRect.origin
                                    radius: [self cornerRadius]];
    [path appendBezierPathWithArcFromPoint: aRect.origin
                                   toPoint: topLeft
                                    radius: [self cornerRadius]];
    [path appendBezierPathWithArcFromPoint: topLeft
                                   toPoint: topRight
                                    radius: [self cornerRadius]];
    [path closePath];
    return [path autorelease];
    
}



+ (float)cornerRadius
{
    return 3.0;
}


- (float)_windowResizeCornerThickness
{
    return [super _windowResizeCornerThickness];
}


+ (NSColor *)fillGradientStartColor
{
	return [NSColor colorWithCalibratedWhite: 197/255.0 alpha: 1.0];
}



+ (NSColor *)fillGradientEndColor
{
	return [NSColor colorWithCalibratedWhite: 140/255.0 alpha: 1.0];
}



+ (NSColor *)edgeGradientStartColor
{
	return [NSColor colorWithCalibratedWhite: 179/255.0 alpha: 1.0];
}



+ (NSColor *)edgeGradientEndColor
{
	return [NSColor colorWithCalibratedWhite: 139/255.0 alpha: 1.0];
}



+ (NSColor *)backgroundColor

{
    return [NSColor colorWithCalibratedWhite: 224/255.0 alpha: 1.0];
}


+ (NSColor *)bottomEdgeColor

{
    return [NSColor colorWithCalibratedWhite: 70/255.0 alpha: 1.0];
}



+ (NSColor *)topWindowEdgeColor

{
    return [NSColor colorWithCalibratedWhite: 222/255.0 alpha: 1.0];
}



+ (NSColor *)bottomWindowEdgeColor

{
    return [NSColor colorWithCalibratedWhite: 65/255.0 alpha: 1.0];
}



+ (NSImage *)gripImage
{
    return [NSImage imageNamed: @"resize"];
}



- (id)initWithFrame:(struct _NSRect)frame styleMask:(unsigned int)style owner:(id)o
{
    self = [super initWithFrame: frame styleMask: style owner: o];
    [self setTitleBarHeight: 22];
    [self setBottomBarHeight: 34];

    titleCell = [[EtchedTextCell alloc] initTextCell: @""];
    [titleCell setFont: [NSFont fontWithName: @"LucidaGrande" size: 13.0]];
	[(EtchedTextCell *)titleCell setShadowColor: [NSColor whiteColor]];
    
    return self;
}



- (void) dealloc
{
    [topRightCorner release];
    [topLeftCorner release];
    [bottomRightCorner release];
    [bottomLeftCorner release];
    [super dealloc];
}


- (NSSize)_topCornerSize
{
    return NSMakeSize([[self class] cornerRadius], [[self class] cornerRadius]);
}


- (NSSize)_bottomCornerSize
{
    return NSMakeSize( [[self class] cornerRadius], [[self class] cornerRadius]);
}


- (void)_drawTitle:(NSRect)rect
{
    NSColor * titleColor;
    if([[self window] isMainWindow])
    {
        titleColor = [NSColor colorWithCalibratedWhite: 0 alpha: 0.9];
    }
    else
    {
        titleColor = [NSColor colorWithCalibratedWhite: 0 alpha: 0.5];
    }
    [self _drawTitleStringIn: rect withColor: titleColor];
}



- (void)_drawTitleBar:(NSRect)rect
{
    [fillGradient fillRect: rect angle: -90.0f];
    [topLeftCorner compositeToPoint: rect.origin operation: NSCompositeSourceOver];
    
    NSPoint rightCornerOrigin = NSMakePoint(NSMaxX(rect) - [topRightCorner size].width, rect.origin.y);
    [topRightCorner compositeToPoint: rightCornerOrigin operation: NSCompositeSourceOver];
    
    [[NSGraphicsContext currentContext] saveGraphicsState];
    [[NSGraphicsContext currentContext] setShouldAntialias: NO];
    
    [NSBezierPath setDefaultLineWidth: 2.0];
    [[[self class] topWindowEdgeColor] set];
    [NSBezierPath strokeLineFromPoint: NSMakePoint( NSMinX(rect) + [topLeftCorner size].width, NSMaxY(rect)) 
                              toPoint: NSMakePoint( NSMaxX(rect) - [topRightCorner size].width, NSMaxY(rect))];
    
    [NSBezierPath setDefaultLineWidth: 1.0];
    [[[self class] bottomEdgeColor] set];
    [NSBezierPath strokeLineFromPoint: NSMakePoint( NSMinX(rect), NSMinY(rect)) 
                              toPoint: NSMakePoint( NSMaxX(rect), NSMinY(rect))];
    
    [[NSGraphicsContext currentContext] restoreGraphicsState];
    
    [self _drawTitle: rect];
}



- (void)_drawBottomBar:(NSRect)rect
{
    [fillGradient fillRect: rect angle: -90.0f];
    
    // Compositing the two corner tiles.
    [bottomLeftCorner compositeToPoint: rect.origin operation: NSCompositeSourceOver];
    NSPoint rightCornerOrigin = NSMakePoint( NSMaxX(rect) - [bottomRightCorner size].width, rect.origin.y);
    [bottomRightCorner compositeToPoint: rightCornerOrigin operation: NSCompositeSourceOver];
    
    [[NSGraphicsContext currentContext] saveGraphicsState];
    [[NSGraphicsContext currentContext] setShouldAntialias: NO];
    
    [[[self class] bottomWindowEdgeColor] set];
    [NSBezierPath setDefaultLineWidth: 1.0];
    [NSBezierPath strokeLineFromPoint: NSMakePoint( NSMinX(rect) + [bottomLeftCorner size].width, NSMinY(rect)) 
                              toPoint: NSMakePoint( NSMaxX(rect) - [bottomRightCorner size].width, NSMinY(rect))];
    
    
    [[[self class] topWindowEdgeColor] set];
    [NSBezierPath setDefaultLineWidth: 1.0];
    [NSBezierPath strokeLineFromPoint: NSMakePoint( NSMinX(rect) , NSMaxY(rect) - 1) 
                              toPoint: NSMakePoint( NSMaxX(rect) , NSMaxY(rect) - 1)];
    
    [[[self class] bottomEdgeColor] set];
    [NSBezierPath setDefaultLineWidth: 1.0];
    [NSBezierPath strokeLineFromPoint: NSMakePoint( NSMinX(rect) , NSMaxY(rect)) 
                              toPoint: NSMakePoint( NSMaxX(rect) , NSMaxY(rect))];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
}



- (void)drawRect:(NSRect)rect
{
    NSRect bounds = [self bounds];
    [[NSColor clearColor] set];
    NSRectFill(rect);

    NSBezierPath *path = [[self class] _clippingPathForFrame: bounds];
    
    [path addClip];

    [[[self class] backgroundColor] set];

    NSRectFill(bounds);
    
    NSRect titleBarRect = bounds;
    titleBarRect.origin.y += NSHeight(bounds) - [self titleBarHeight];
    titleBarRect.size.height = [self titleBarHeight];
    
    NSRect bottomBarRect = bounds;
    bottomBarRect.size.height = [self bottomBarHeight];

    [self _drawTitleBar: titleBarRect];
    [self _drawBottomBar: bottomBarRect];
}



- (NSRect)_growBoxRect
{
    NSRect growRect = NSZeroRect;
    if ([[self window] isResizable])
    {
        NSSize gripSize = NSMakeSize(12,12);//[[[self class] gripImage] size];
        growRect = NSMakeRect(NSMaxX([self frame]) - gripSize.width , 
                              NSMinY([self frame]),
                              gripSize.width, 
                              gripSize.height);
    }
    return growRect;
}



- (NSPoint)_closeButtonOrigin
{
    NSPoint origin = [super _closeButtonOrigin];
    ++origin.y;
    return origin;
}



//- (NSPoint)_zoomButtonOrigin
//{
//    NSPoint origin = [super _zoomButtonOrigin];
//    origin.y += 1.5;
//    return origin;
//}
//
//
//
//- (NSPoint)_collapseButtonOrigin
//{
//    NSPoint origin = [super _collapseButtonOrigin];
//    origin.y += 1.5;
//    return origin;
//}



- (void)_drawGrowBoxWithClip:(NSRect)rect
{
    NSImage * resize = [[self class] gripImage];
    rect.origin.x += 2;
    rect.origin.y += 3;
    [resize compositeToPoint: rect.origin operation: NSCompositeSourceOver];
}



- (float)titleBarHeight
{
    if([self _toolbarIsShown])
    {
        return mTitleBarHeight + [self _distanceFromToolbarBaseToTitlebar];
    }
    return mTitleBarHeight;
}



- (void)setTitleBarHeight:(float)height
{
    mTitleBarHeight = height;
    [topLeftCorner release];
    [topRightCorner release];
    NSSize cornerSize = NSMakeSize([[self class] cornerRadius], height);
    topLeftCorner = [[NSImage alloc] initWithSize: cornerSize];

    NSBezierPath * cornerBorder = [NSBezierPath new];
    [cornerBorder moveToPoint: NSZeroPoint];
    [cornerBorder appendBezierPathWithArcFromPoint: NSMakePoint(0, height) toPoint: NSMakePoint(cornerSize.width, height) 
                                      radius: cornerSize.width];
    [cornerBorder lineToPoint: NSMakePoint(cornerSize.width, 0)];
    [cornerBorder closePath];
    
    NSBezierPath * cornerFill = [NSBezierPath new];
    [cornerFill moveToPoint: NSZeroPoint];
    [cornerFill appendBezierPathWithArcFromPoint: NSMakePoint(0, height - 1) toPoint: NSMakePoint(cornerSize.width, height - 1) 
                                            radius: cornerSize.width];
    [cornerFill lineToPoint: NSMakePoint(cornerSize.width, 0)];
    [cornerFill closePath];

    [topLeftCorner lockFocus];
        [[[self class] topWindowEdgeColor] set];
        [cornerBorder stroke];
        [fillGradient fillBezierPath: cornerFill angle: -90];
    [topLeftCorner unlockFocus];
    
    [cornerBorder release];
    [cornerFill release];
    
    cornerBorder = [NSBezierPath new];
    [cornerBorder moveToPoint: NSMakePoint(cornerSize.width, 0)];
    [cornerBorder appendBezierPathWithArcFromPoint: NSMakePoint(cornerSize.width, height) toPoint: NSMakePoint(0, height) radius: cornerSize.width];
    [cornerBorder lineToPoint: NSZeroPoint];
    [cornerBorder closePath];
    
    cornerFill = [NSBezierPath new];
    [cornerFill moveToPoint: NSMakePoint(cornerSize.width, 0)];
    [cornerFill appendBezierPathWithArcFromPoint: NSMakePoint(cornerSize.width, height - 1) toPoint: NSMakePoint(0, height - 1) radius: cornerSize.width];
    [cornerFill lineToPoint: NSZeroPoint];
    [cornerFill closePath];
    
    topRightCorner = [[NSImage alloc] initWithSize: cornerSize];
    [topRightCorner lockFocus];
        [[[self class] topWindowEdgeColor] set];
        [cornerBorder fill];
        [fillGradient fillBezierPath: cornerFill angle: -90];
    [topRightCorner unlockFocus];
    
    [cornerBorder release];
    [cornerFill release];
    
    [self setNeedsDisplay:YES];
}



- (float)bottomBarHeight
{
    return mBottomBarHeight;
}



- (void)setBottomBarHeight:(float)height
{
    mBottomBarHeight = height;
    
    [bottomLeftCorner release];
    [bottomRightCorner release];
    
    NSSize cornerSize = NSMakeSize([[self class] cornerRadius], height);
    bottomLeftCorner = [[NSImage alloc] initWithSize: cornerSize];
    
    NSBezierPath * cornerBorder = [NSBezierPath new];
    [cornerBorder moveToPoint: NSMakePoint(0, height)];
    [cornerBorder appendBezierPathWithArcFromPoint: NSZeroPoint toPoint: NSMakePoint(cornerSize.width, 0) radius: cornerSize.width];
    [cornerBorder lineToPoint: NSMakePoint(cornerSize.width, height)];
    [cornerBorder closePath];
    
    NSBezierPath * cornerFill = [NSBezierPath new];
    [cornerFill moveToPoint: NSMakePoint(0, height)];
    [cornerFill appendBezierPathWithArcFromPoint: NSMakePoint(0, 1) toPoint: NSMakePoint(cornerSize.width, 1) radius: cornerSize.width];
    [cornerFill lineToPoint: NSMakePoint(cornerSize.width, height)];
    [cornerFill closePath];
    
    [bottomLeftCorner lockFocus];
        [[[self class] bottomWindowEdgeColor] set];
        [cornerBorder fill];
        [fillGradient fillBezierPath: cornerFill angle: -90];
    [bottomLeftCorner unlockFocus];
	[cornerBorder release];
	[cornerFill release];
	
    cornerBorder = [NSBezierPath new];
    [cornerBorder moveToPoint: NSMakePoint(cornerSize.width, height)];
    [cornerBorder appendBezierPathWithArcFromPoint: NSMakePoint(cornerSize.width, 0) toPoint: NSZeroPoint radius: cornerSize.width];
    [cornerBorder lineToPoint: NSMakePoint(0, height)];
    [cornerBorder closePath];
    
    cornerFill = [NSBezierPath new];
    [cornerFill moveToPoint: NSMakePoint(cornerSize.width, height)];
    [cornerFill appendBezierPathWithArcFromPoint: NSMakePoint(cornerSize.width, 1) toPoint: NSMakePoint(0,1) radius: cornerSize.width];
    [cornerFill lineToPoint: NSMakePoint(0, height)];
    [cornerFill closePath];
    
    bottomRightCorner = [[NSImage alloc] initWithSize: cornerSize];
    [bottomRightCorner lockFocus];
        [[[self class] bottomWindowEdgeColor] set];
        [cornerBorder fill];
        [fillGradient fillBezierPath: cornerFill angle: -90];
    [bottomRightCorner unlockFocus];
	[cornerBorder release];
	[cornerFill release];
	
    [self setNeedsDisplay:YES];
}



- (NSRect)contentRectForFrameRect:(NSRect)frameRect styleMask:(unsigned int)aStyle
{
    frameRect.size.height -= [self titleBarHeight];
    return frameRect;
}



- (NSRect)frameRectForContentRect:(NSRect)windowContent styleMask:(unsigned int)aStyle

{
    windowContent.size.height += [self titleBarHeight];
    return windowContent;
}



- (void)_showToolbarWithAnimation:(BOOL)animate
{
    [super _showToolbarWithAnimation: animate];
    [self setNeedsDisplay:YES];
}


@end